import { CreateTokenDto } from '@/common/dto/CreateTokenDto';
import { ResultData } from '@/common/dto/result';
import { ApiResult } from '@/decorators/apiResult-decorators';
import { Body, Controller, Post, Req } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiExtraModels, ApiOperation, ApiTags } from '@nestjs/swagger';
import { UsersService } from './users.service';
import { AllowAnon } from '@/decorators/allow-anon.decortors';
import { LoginUserDto } from './dto/UserDto';

@ApiTags('基本信息管理')
// ApiExtraModels 作用：指定一个或多个模型，它们将被添加到Swagger文档中
@ApiExtraModels(ResultData, CreateTokenDto)
@Controller()
export class BaseContolller {
  constructor(private readonly usersService: UsersService) {}
  @Post('login')
  @ApiOperation({ summary: '员工登录' })
  @ApiResult(CreateTokenDto)
  @AllowAnon()
  // Pick<LoginUser, 'username' | 'password'>
  async login(@Body() dto: LoginUserDto): Promise<ResultData> {
    return this.usersService.login(dto.username, dto.password);
  }

  @Post('update/token')
  @ApiOperation({ summary: '刷新token' })
  @ApiResult(CreateTokenDto)
  @ApiBearerAuth()
  async updateToken(@Req() req): Promise<ResultData> {
    return this.usersService.updateToken(req.user.id);
  }
}
